/*
* @Author: dengjiayao
* @Date:   2017-08-25 15:34:01
* @Last Modified by:   dengjiayao
* @Last Modified time: 2017-10-24 11:19:49
*/
import Pages from '../../component/pages/pages.vue'

import {
  showMsg,
  getVoucherAuditList,
  auditMoneyVoucher
} from '../../vuex/action'

export default {
  name: 'voucher-audit-list',
  components: {
    'vue-pages': Pages
  },
  data() {
    return {
      list: [],
      pages: {
        start: 0,
        totalPages: 0,
        limit: 20
      },
      filter: {
        fundId: '',
        status: ''
      }
    }
  },
  vuex: {
    getters: {
      fresh: state => state.voucherAuditNeedFresh
    },
    actions: {
      xShowMsg: showMsg,
      xGetList: getVoucherAuditList,
      xAudit: auditMoneyVoucher
    }
  },
  created() {
    this.getList({})
  },
  methods: {
    getList({ start = this.pages.start, limit = this.pages.limit }) {
      let params = {
        start: start,
        limit: limit
      }
      if (this.filter.status !== '') {
        params.status = this.filter.status
      }
      if (this.filter.fundId) {
        params.fundId = this.filter.fundId
      }
      this.xGetList(params)
        .then(data => {
          this.list = data.list
          this.pages.totalPages = Math.ceil(data.count / limit)
        })
        .catch(res => {
          this.xShowMsg({
            type: 'danger',
            content: res.error_msg
          })
        })
    },
    searchList() {
      this.pages.start = 0
      this.getList({})
    },
    updateItem({ id, fundId, realTranamt, remark, moneyChannel }, status) {
      if (+status === 1 && realTranamt.length === 0) {
        return this.xShowMsg({
          type: 'danger',
          content: '请填写实际到账金额'
        })
      }
      if (+status === -1 && (!remark || remark.length === 0)) {
        return this.xShowMsg({
          type: 'danger',
          content: '请填写备注'
        })
      }
      let params = {
        id: id,
        fundId: fundId,
        status: status
      }
      if (+status === 1) {
        params.realTranamt = realTranamt
      }
      if (+status === -1 || +status === 1) {
        params.remark = remark
      }
      if (moneyChannel) {
        params.moneyChannel = moneyChannel
      }
      this.xAudit(params).catch(res => {
        this.xShowMsg({
          type: 'danger',
          content: res.error_msg
        })
      })
    }
  },
  watch: {
    fresh(val) {
      if (val) {
        this.getList({})
      }
    }
  }
}
